package com.example.myc1;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

    private String[] titles = { "桌子", "苹果", "蛋糕", "线衣", "猕猴桃", "围巾",
            "桌子", "苹果", "蛋糕", "线衣", "猕猴桃", "围巾",
            "桌子", "苹果", "蛋糕", "线衣", "猕猴桃", "围巾"};
    private String[] prices = { "660元", "10元/kg", "300元", "350元", "10元/kg", "280元",
            "1800元", "10元/kg", "300元", "260元", "10元/kg", "280元",
            "1800元", "10元/kg", "300元", "350元", "10元/kg", "280元"};
    private int[] icons = {R.drawable.table,R.drawable.apple,R.drawable.cake,
            R.drawable.wireclothes,R.drawable.kiwifruit,R.drawable.scarf,
            R.drawable.table,R.drawable.apple,R.drawable.cake,
            R.drawable.wireclothes,R.drawable.kiwifruit,R.drawable.scarf,
            R.drawable.table,R.drawable.apple,R.drawable.cake,
            R.drawable.wireclothes,R.drawable.kiwifruit,R.drawable.scarf};

    private ListView lvHome;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        lvHome = findViewById(R.id.lv_home);
        GoodsAdapter goodsAdapter = new GoodsAdapter();
        lvHome.setAdapter(goodsAdapter);
    }


    private class GoodsAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Object getItem(int position) {
            return titles[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view = View.inflate(MainActivity.this, R.layout.item_layout, null);

            TextView title = view.findViewById(R.id.title_item);
            TextView price = view.findViewById(R.id.price_item);
            ImageView image = view.findViewById(R.id.image_item);

            title.setText(titles[position]);
            price.setText(prices[position]);
            image.setBackgroundResource(icons[position]);

            return view;
        }
    }
}